<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title>详情页</title>
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

</head>
<body>
<%-- 创建全局提示框 --%>
<div class="globalAlert"
     style="position: absolute;top:150px;left: 50%;width: 25%; z-index: 1000;text-align: center;vertical-align: center">
    <div class="alert alert-success" role="alert" style="height: 50px;" id="alert-success"></div>
    <div class="alert alert-info" role="alert" style="height: 50px;" id="alert-info"></div>
    <div class="alert alert-warning" role="alert" style="height: 50px;" id="alert-warning"></div>
    <div class="alert alert-danger" role="alert" style="height: 50px;" id="alert-danger"></div>
</div>

<!-- 修改市场活动备注的模态窗口 -->
<div class="modal fade" id="editRemarkModal" role="dialog">
    <%-- 备注的id --%>
    <input type="hidden" id="remarkId">
    <div class="modal-dialog" role="document" style="width: 40%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">修改备注</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <label for="edit-id"></label>
                    <input hidden type="text" id="edit-id"/>
                    <div class="form-group">
                        <label for="edit-describe" class="col-sm-2 control-label">内容</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <label for="noteContent"></label>
                            <textarea class="form-control" rows="3" id="noteContent"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="updateRemarkBtn">更新</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改市场活动的模态窗口 -->
<div class="modal fade" id="editActivityModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">修改市场活动</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal" role="form">

                    <div class="form-group">
                        <label for="edit-marketActivityOwner" class="col-sm-2 control-label">所有者<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control" id="edit-marketActivityOwner">
                                <option>zhangsan</option>
                                <option>lisi</option>
                                <option>wangwu</option>
                            </select>
                        </div>
                        <label for="edit-marketActivityName" class="col-sm-2 control-label">名称<span
                                style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-marketActivityName" value="发传单">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-startTime" class="col-sm-2 control-label">开始日期</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-startTime" value="2020-10-10">
                        </div>
                        <label for="edit-endTime" class="col-sm-2 control-label">结束日期</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-endTime" value="2020-10-20">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-cost" class="col-sm-2 control-label">成本</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-cost" value="5,000">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-describe" class="col-sm-2 control-label">描述</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control" rows="3" id="edit-describe">市场活动Marketing，是指品牌主办或参与的展览会议与公关市场活动，包括自行主办的各类研讨会、客户交流会、演示会、新产品发布会、体验会、答谢会、年会和出席参加并布展或演讲的展览会、研讨会、行业交流会、颁奖典礼等</textarea>
                        </div>
                    </div>

                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">更新</button>
            </div>
        </div>
    </div>
</div>

<!-- 返回按钮 -->
<div style="position: relative; top: 35px; left: 10px;">
    <a href="javascript:void(0);" onclick="window.history.back();"><span class="glyphicon glyphicon-arrow-left"
                                                                         style="font-size: 20px; color: #DDDDDD"></span></a>
</div>

<!-- 大标题 -->
<div style="position: relative; left: 40px; top: -30px;">
    <div class="page-header">
        <h3>市场活动-${activity.name}<small> &nbsp;${activity.startDate} ~ ${activity.endDate}</small></h3>
    </div>
    <div style="position: relative; height: 50px; width: 250px;  top: -72px; left: 700px;">
        <button type="button" class="btn btn-default" data-toggle="modal" data-target="#editActivityModal"><span
                class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-minus"></span> 删除</button>
    </div>
</div>

<!-- 详细信息 -->
<div style="position: relative; top: -70px;">
    <div style="position: relative; left: 40px; height: 30px;">
        <div hidden id="detail-id">${activity.id}</div>
        <div style="width: 300px; color: gray;">所有者</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${activity.owner}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">名称</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${activity.name}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>

    <div style="position: relative; left: 40px; height: 30px; top: 10px;">
        <div style="width: 300px; color: gray;">开始日期</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${activity.startDate}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">结束日期</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${activity.endDate}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 20px;">
        <div style="width: 300px; color: gray;">成本</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${activity.cost}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 30px;">
        <div style="width: 300px; color: gray;">创建者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;">
            <b>${activity.createBy}</b>&nbsp;&nbsp;<small
                style="font-size: 10px; color: gray;">${activity.createTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 40px;">
        <div style="width: 300px; color: gray;">修改者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;">
            <b>${activity.editBy}</b>&nbsp;&nbsp;<small
                style="font-size: 10px; color: gray;">${activity.editTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 50px;">
        <div style="width: 300px; color: gray;">描述</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>${activity.description}</b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
</div>

<!-- 备注 -->
<div style="position: relative; top: 30px; left: 40px;" id="remarkBody">
    <div class="page-header">
        <h4>备注</h4>
    </div>

    <div id="activityRemark">

    </div>
    <div id="remarkDiv" style="background-color: #E6E6E6; width: 870px; height: 90px;">
        <form role="form" style="position: relative;top: 10px; left: 10px;">
            <label for="remark"></label>
            <textarea id="remark" class="form-control" style="width: 850px; resize : none;" rows="2"
                      placeholder="添加备注..."></textarea>
            <p id="cancelAndSaveBtn" style="position: relative;left: 737px; top: 10px; display: none;">
                <button id="cancelBtn" type="button" class="btn btn-default">取消</button>
                <button type="button" class="btn btn-primary" id="saveRemarkBtn">保存</button>
            </p>
        </form>
    </div>
</div>
<div style="height: 200px;"></div>

<script type="text/javascript">
    const alert_success = "success";
    const alert_info = "info";
    const alert_warning = "warning";
    const alert_danger = "danger";

    //默认情况下取消和保存按钮是隐藏的
    var cancelAndSaveBtnDefault = true;

    $(function () {

        // 隐藏提示框
        $("#alert-success").hide();
        $("#alert-info").hide();
        $("#alert-warning").hide();
        $("#alert-danger").hide();

        $("#remark").focus(function () {
            if (cancelAndSaveBtnDefault) {
                //设置remarkDiv的高度为130px
                $("#remarkDiv").css("height", "130px");
                //显示
                $("#cancelAndSaveBtn").show("2000");
                cancelAndSaveBtnDefault = false;
            }
        });

        $("#cancelBtn").click(function () {
            //显示
            $("#cancelAndSaveBtn").hide();
            //设置remarkDiv的高度为130px
            $("#remarkDiv").css("height", "90px");
            cancelAndSaveBtnDefault = true;
        });

        $("#remarkBody").on("mouseover", ".remarkDiv", function () {
            $(this).children("div").children("div").show();
        });
        $("#remarkBody").on("mouseout", ".remarkDiv", function () {
            $(this).children("div").children("div").hide();
        });

        // 刷新市场活动备注活动列表
        getActivityRemarkList();

        $("#saveRemarkBtn").click(function () {
            //
            $.ajax({
                url: "workbench/activity/saveRemark.do",
                data: {
                    "activityId": $("#detail-id").html(),
                    "noteContent": $("#remark").val()
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    // data: {"result":true/false, "activityRemark":activityRemark}
                    if (data.result) {

                        globalAlert(alert_success, "保存成功")
                        let html = "";
                        html += '<div id="' + data.ar.id + '" class="remarkDiv" style="height: 60px;">';
                        html += '<img title="" src="image/user-thumbnail.png" style="width: 30px; height:30px;">';
                        html += '<div style="position: relative; top: -40px; left: 40px;">';
                        html += '<h5>' + data.ar.noteContent + '</h5>';
                        html += '<font color="gray">市场活动</font> <font color="gray">-</font> <b>${activity.name}</b>';
                        html += '<small style="color: gray;">' + data.ar.createTime + '';
                        html += '由' + data.ar.createBy + '</small>';
                        html += '<div style="position: relative; left: 500px; top: -30px; height: 30px; width: 100px; display: none;">';
                        html += '<a class="myHref" href="javascript:void(0);">';
                        html += '<span class="glyphicon glyphicon-edit" style="font-size: 20px; color: #FF0000;"></span></a>';
                        html += '&nbsp;&nbsp;&nbsp;&nbsp;';
                        html += '<a class="myHref" href="javascript:void(0);" onclick="deleteRemark(\'' + data.ar.id + '\')">';
                        html += '<span class="glyphicon glyphicon-remove" style="font-size: 20px; color: #FF0000;"></span></a>';
                        html += '</div></div></div>';

                        $("#remarkDiv").before(html);

                        // 清空文本域
                        $("#remark").val("");

                    } else {
                        globalAlert(alert_danger, "保存失败");
                    }
                }
            });
        })
    });

    function globalAlert(type, msg) {
        // 设置全局提示框
        let html = "";
        if (type === "success") {
            html = '<span class="glyphicon glyphicon-ok-sign">' + msg + '</span>'
            $("#alert-success").html(html);
            $("#alert-success").slideDown();
            $("#alert-success").delay(3000);
            $("#alert-success").slideUp();

        } else if (type === "info") {
            html = '<span class="glyphicon glyphicon-info-sign">' + msg + '</span>'
            $("#alert-info").html(html);
            $("#alert-info").slideDown();
            $("#alert-info").delay(3000);
            $("#alert-info").slideUp();

        } else if (type === "warning") {
            html = '<span class="glyphicon glyphicon-exclamation-sign">' + msg + '</span>'
            $("#alert-warning").html(html);
            $("#alert-warning").slideDown();
            $("#alert-warning").delay(3000);
            $("#alert-warning").slideUp();

        } else if (type === "danger") {
            html = '<span class="glyphicon glyphicon-remove-sign">' + msg + '</span>'
            $("#alert-danger").html(html);
            $("#alert-danger").slideDown();
            $("#alert-danger").delay(3000);
            $("#alert-danger").slideUp();

        } else {
            html = '<span class="glyphicon glyphicon-remove-sign">输入提示类型错误!</span>'
            $("#alert-danger").html();
            $("#alert-danger").slideDown();
            $("#alert-danger").delay(3000);
            $("#alert-danger").slideUp();

        }
    }

    function getActivityRemarkList() {
        // 查询市场活动备注列表
        $.ajax({
            url: "workbench/activity/getActivityRemarkList.do",
            data: {
                "activityId": $("#detail-id").html()
            },
            type: "get",
            dataType: "json",
            success: function (data) {

                let html = "";
                $.each(data, function (index, item) {
                    let time = item.editFlag === "0" ? item.createTime : item.editTime;
                    let user = item.editFlag === "0" ? item.createBy : item.editBy;
                    html += '<div id="' + item.id + '" class="remarkDiv" style="height: 60px;">';
                    html += '<img title="" src="image/user-thumbnail.png" style="width: 30px; height:30px;">';
                    html += '<div style="position: relative; top: -40px; left: 40px;">';
                    html += '<h5>' + item.noteContent + '</h5>';
                    html += '<font color="gray">市场活动</font> <font color="gray">-</font> <b>${activity.name}</b>';
                    html += '<small style="color: gray;" id="edit-' + item.id + '">' + time + '';
                    html += '由' + user + '</small>';
                    html += '<div style="position: relative; left: 500px; top: -30px; height: 30px; width: 100px; display: none;">';
                    html += '<a class="myHref" href="javascript:void(0);" onclick="editRemark(\'' + item.id + '\', \'' + item.noteContent + '\')">';
                    html += '<span class="glyphicon glyphicon-edit" style="font-size: 20px; color: #FF0000;"></span></a>';
                    html += '&nbsp;&nbsp;&nbsp;&nbsp;';
                    html += '<a class="myHref" href="javascript:void(0);" onclick="deleteRemark(\'' + item.id + '\')">';
                    html += '<span class="glyphicon glyphicon-remove" style="font-size: 20px; color: #FF0000;"></span></a>';
                    html += '</div></div></div>';
                });
                // 在该元素前追加内容
                $("#activityRemark").html(html);
            },
            error: function () {
                globalAlert(alert_warning, "出错了")
            }
        });

        $("#updateRemarkBtn").click(function () {
            $.ajax({
                url: "workbench/activity/updateRemark.do",
                data: {
                    "id": $("#edit-id").val(),
                    "noteContent": $("#noteContent").val()
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.result) {
                        globalAlert(alert_success, "修改成功");
                        $("#editRemarkModal").modal("hide");
                        $("#noteContent").val("");
                        getActivityRemarkList();
                    } else {
                        globalAlert(alert_danger, "更新失败");
                    }
                }
            });
        })

    }

    function editRemark(id, noteContent) {
        $("#edit-id").val(id);
        $("#noteContent").val(noteContent);
        $("#editRemarkModal").modal("show");
    }

    function deleteRemark(id) {
        $.ajax({
            url: "workbench/activity/deleteRemark.do",
            data: {
                "id": id
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.result) {
                    //
                    globalAlert(alert_success, "删除成功");
                    $("#" + id).remove();
                } else {
                    globalAlert(alert_danger, "删除失败");
                }
            }
        });
    }

</script>
</body>
</html>